import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class BoundGateways extends StatefulWidget {
  final int lockId;
  const BoundGateways({super.key, required this.lockId,});

  @override
  State<StatefulWidget> createState() => _BoundGatewaysState();

}

class _BoundGatewaysState extends State<BoundGateways> {

  List _list = [];

  Future<void> _getGatewayList() async {
    EasyLoading.show();
    Map resData = await lockApi.getGatewayList(widget.lockId);
    EasyLoading.dismiss();
    setState(() {
      _list = resData["rows"] ?? [];
    });
  }

  @override
  void initState() {
    _getGatewayList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("公寓名称", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView.separated(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        itemBuilder: (context, index) {
          if (index == 0) {
            return const Text("锁连接的网关", style: TextStyle(fontSize: 16),);
          }
          Map item = _list[index - 1];
          return Container(
            padding: const EdgeInsets.all(12),
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(5))
            ),
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(8),
                  decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(50)),
                      boxShadow: [BoxShadow(spreadRadius: 2, color: ThemeVars.colorPrimaryLight)],
                      color: Colors.white
                  ),
                  child: const Text("G2", style: TextStyle(fontSize: 20, color: ThemeVars.colorPrimary, fontWeight: FontWeight.w500),),
                ),
                const SizedBox(width: 16,),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(item["gatewayName"], style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),),
                    Text(item["rssi"] > -60 ? "信号强" : "信号弱", style: TextStyle(color: ThemeVars.colorTextTer, fontSize: 12),)
                  ],
                )
              ],
            ),
          );
        },
        separatorBuilder: (context, index) => const SizedBox(height: 8,),
        itemCount: _list.length + 1,
      ),
    );
  }

}